package com.itheima.test7;

import java.io.*;
import java.net.Socket;

/*
上传图片


接受 服务端给的反馈
 */
public class ClientTest {

    public static void main(String[] args) throws IOException {


        for (int i=0;i<100;i++) {
            // 创建客户端对象

            Runnable runnable = new Runnable() {
                 Socket socket = new Socket("127.0.0.1", 10001);
                public void run() {
                    BufferedInputStream bis = null;
                    try {
                        //创建输入流 用来读数据
                        bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\美女.jpg"));
                        //获取输出流
                        OutputStream os = socket.getOutputStream();
                        BufferedOutputStream bos = new BufferedOutputStream(os);
                        //边读 编写
                        byte[] bytes= new byte[1024];
                        int len;
                        while ((len=bis.read(bytes))!=-1){
                            //bytes  0.len
                            bos.write(bytes,0,len);
                        }
                        bos.flush();
                        //给服务器一个结束标记
                        socket.shutdownOutput();

                        //接受服务器的响应
                        InputStream is = socket.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        String s = reader.readLine();

                        //读 服务器响应的数据
                        System.out.println(s);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        // 释放资源
                        if (socket!=null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (bis!=null) {
                            try {
                                bis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            };
            new  Thread(runnable).start();
        }


    }
}
